import {
  createRouter,
  createWebHashHistory,
  type RouteRecordRaw,
} from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import ErrorView from "@/views/error/404.vue";
import LayoutMainVue from "@/layout/layoutMain.vue";
import layoutSider from "@/layout/layoutSider.vue";
import admin from "./admin";
import platform from "./platform";
NProgress.configure({ easing: "ease", speed: 500, showSpinner: false });
const onChildRoute = (list: RouteRecordRaw[], str?: string) => {
  list = list.map((router) => {
    if (router.children) {
      return {
        ...router,
        path: `${str ? "/" + str : undefined}${router.path}`,
        name: `${str}_${String(router.name)}`,
        children: onChildRoute(router.children, str),
      };
    }
    return {
      ...router,
      name: `${str}_${String(router.name)}`,
      path: `${str ? "/" + str : undefined}${router.path}`,
    };
  });
  return list;
};
let routes: RouteRecordRaw[] = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/",
    component: LayoutMainVue,
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("@/views/home/index.vue"),
      },
    ],
  },
  {
    path: "/admin",
    name: "admin",
    component: layoutSider,
    children: onChildRoute(admin, "admin"),
  },
  {
    path: "/main",
    name: "main",
    component: LayoutMainVue,
    children: onChildRoute(platform, "main"),
  },
  {
    path: "/menu",
    name: "menu",
    component: layoutSider,
    children: onChildRoute(platform, "menu"),
  },

  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    component: ErrorView,
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach((to, from, next) => {
  NProgress.start();
  next();
});
router.afterEach(() => {
  NProgress.done();
});
export default router;
